package com.qianfeng.shixu.dao;

import org.apache.commons.dbcp.BasicDataSourceFactory;
import org.junit.After;
import org.junit.Test;

import javax.sql.DataSource;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

/*
提供一个工具类,用来得到连接对象Connection,
 */
public class DBUtil {
    private static Connection connection;
    static DataSource dataSource=null;
    static {

        Properties properties = new Properties();
        try {
            //获取配置信息,将内容保存到properties对象里面
                properties.load( DBUtil.class.getClassLoader().getResourceAsStream( "dbcp.properties" ) );
        } catch (IOException e) {
            e.printStackTrace();
        }

        //得到连接池对象,将配置信息指定给连接池对象
        try {
            dataSource = BasicDataSourceFactory.createDataSource( properties );
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void setConnection(Connection connection) {
        DBUtil.connection = connection;
    }

    public void after() {
        try {
            getConnection().close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}